<?php
require './include/global.php';
require './include/validate.php';
require './include/all_left.php';
accessblock(array("Trial"));

require_once './include/advisory_db.php';

$smarty->compile_check = true;
$smarty->display('header.tpl');

if($_POST[jid]){
	$postid    = explode("|",$_POST[jid]);
	$_GET[jid] = $postid[0];
	$onlyreid  = $postid[1];
	$smarty->assign('onlyreid',$onlyreid);
}
$jid   = trim($_GET[jid]) + 0;

//check if the guy has the process
$sql = "SELECT COUNT(*) as count FROM pa_contact WHERE re_aliasid = '".$jid."' AND re_uid = '".$_SESSION[uid]."' AND re_isapproved = '1' ";
$_condition = $DB->fetch_one_array($sql);
$_error = false;
if($_condition['count'] > 0 || $_SESSION['status'] == "Premium"){
	$_error = true;
}
//check if they are friends
$sql = "SELECT jo_uid FROM pa_job WHERE jo_id = '".$jid."'";
$jobInfo = $DB->fetch_one_array($sql);
$areFriends = (areFriends($_SESSION['uid'],$jobInfo['jo_uid']))?1:0;
if($areFriends == 1){
	$_error = true;
}

if($_error == false){
   echo "<meta http-equiv=\"refresh\" content=\"0;URL=errorall.php?errid=rc_1014\">";
   exit;	
}


//BEGIN: add for Recruiter is not able to VIEW their own candidate record by aaron on 2011-12-26
$usertype = $_SESSION['usertype'];
$smarty->assign('usertype',$usertype);
//END: add for Recruiter is not able to VIEW their own candidate record by aaron on 2011-12-26
$smarty->assign('jid',$jid);

$sql    = "SELECT * FROM pa_job WHERE jo_id = '".$jid."'";
//echo $sql;
$jobarr = $DB->fetch_one_array($sql);
if($jobarr[jo_uid]==$_SESSION[uid] || $jobarr[jo_status] == 3){
   $err = "Invalid ID!";
   $smarty->assign("err",$err);
   $smarty->display("left.tpl");
   $smarty->display("error.tpl");
   $smarty->display("right.tpl");
   $smarty->display('footer.tpl');
   exit;
}
//print_r($jobarr);
if($jobarr[jo_updtime]=='0') $jobarr[jo_updtime] = $jobarr[jo_regtime];
$jobarr[jo_updtime] = date("m/d/Y",$jobarr[jo_updtime]);
$jobarr[jo_regtime] = date("m/d/Y",$jobarr[jo_regtime]);
$jobarr[jo_status]  = $jobfillstatus[$jobarr[jo_status]];

$smarty->assign("jobarr",$jobarr);
$smarty->assign("fee_options",$fee_options);




if($_GET[country]==''){
$_GET[country] = 211;
}

$smarty->assign('s',$_GET);
//jobupdate
$smarty->assign('jobupdate_names',$UPDATE);
$smarty->assign('state',$state);
$smarty->assign('country',$country);
$smarty->assign('industry',$jobcategory);
$smarty->assign('function',$jobfunction);
$smarty->assign('salary',$salary);
$smarty->assign('jobtype',$jobtype);
$smarty->assign('rec_findapplicant',$all_submitapplicant);
//only resumeid

//print_r($findresume_orderby);
$where = "  WHERE `re_usertype`='".$_SESSION[usertype]."' AND `re_uid`='".$_SESSION[uid]."' ";
if($_GET[action]!='search' && $_GET[fid]==''&&empty($_POST[jid])) $where .= " AND re_aliasid = '0'  ";
$order = "  ORDER BY re_id DESC ";
if($onlyreid!=''){ $where .= " AND re_id = '".$onlyreid."' ";}

if($_GET[action]=="search"){ 

	if($_GET[keywords]!=''){ $arr[] = " (`re_jobtitle`  like '%".$_GET[keywords]."%'   OR `re_detail` like '%".$_GET[keywords]."%') ";}	
	if($_GET[orderby]=='0'){$by = "re_id";}
	else{
		$ord   = array('','re_regtime','re_fname','re_yearexp','re_edulevel','re_desiredpay');
		$by = $ord[$_GET[orderby]];
	}
    if(empty($by)){ $by = "re_id";}
	if($_GET[name]!='')      { $arr[] = " (`re_fname` like '%".$_GET[name]."%' OR `re_lname` like '%".$_GET[name]."%') " ;}
	if($_GET[city]!='')      { $arr[] = " `re_locity` like '%".$_GET[city]."%' ";}
    if($_GET[state]!='0')    { $arr[] = " `re_lostate` = '".$_GET[state]."'  ";}
	if($_GET[country]!='0')  { $arr[] = " `re_locountry` = '".$_GET[country]."'  ";}
	if($_GET[industry]!='0') { $arr[] = " `re_jobindustry` = '".$_GET[industry]."'  ";}
	if($_GET[jobtitle]!='')  { $arr[] = " `re_jobtitle`  like '%".$_GET[jobtitle]."%'  ";}	
	if($_GET[jobtype]!='0' ) { $arr[] = " `re_jobtype`  =  '".$_GET[jobtype]."'  ";}
	if($_GET[certif]!='' )   { $arr[] = " `re_proflicense` like '%".$_GET[certif]."%'  ";}
    if($_GET[postdate]!='')  { 
			 $dateadd = strtotime($_GET['postdate'])+86400;
		     $arr[]   = " (`re_regtime` > '".strtotime($_GET['postdate'])."' AND `re_regtime`<'".$dateadd."')  ";
	}
	if($_GET[alias]!=1)      { $arr[] = "  re_aliasid = '0'  "; }elseif($_GET[orderby]=='0'){ $by = " re_fname ";}

	//print_r($arr);
	if(!empty($arr)){
		$implode  = @implode("AND", $arr);
		$implode  = " AND ".$implode;
		$where    .= $implode;
	}
    $order = " GROUP BY re_fname,re_aliasname ORDER BY $by ASC";
}

$nav = new buildNav;
$total = $DB->fetch_one_array("SELECT COUNT(*) AS count FROM pa_resume $where");
$nav->total_result = $total[count];
$nav->limit = 5;
$nav->execute("SELECT * FROM pa_resume $where $order");

$page = $nav->pagenav();
//echo $sql2;

//test in applicant----------------------
 $sql = "SELECT * FROM pa_applicant WHERE app_recruitid = '".$_SESSION[uid]."' AND app_jobid='".$jid."' ";
 $result = $DB->query($sql);
 $in_apparr = array();
 while($list = $DB->fetch_array($result)){
    $in_apparr[] = $list[app_resumeid];
 }

//tested

$i=0;
while($list2 = $DB->fetch_array($nav->sql_result)){
   $dataresume[$i]              = $list2;
   $dataresume[$i][name]        = $list2[re_fname]." ".$list2[re_lname] ;
   $dataresume[$i][re_regtime]  = @date("m/d/Y",$list2[re_regtime]);
   $dataresume[$i][re_yearexp]  = ($list2[re_yearexp]>0)?$yearexp2[$list2[re_yearexp]]:"";
   $dataresume[$i][re_edulevel] = ($list2[re_edulevel]>0)?$education[$list2[re_edulevel]]:"";
   $dataresume[$i][re_desiredpay] = number_format($list2[re_desiredpay]);
   $dataresume[$i][in_app]  = (@in_array($dataresume[$i][re_id],$in_apparr))?1:0;

  //C&A information
   $sql = "SELECT in_status FROM pa_invite WHERE in_jobid = '".$jid."' AND in_resumeid = '".$list2[re_id]."' AND in_recruiterid ='".$_SESSION[uid]."' ";
   $rs  = $DB->fetch_one_array($sql);
   $dataresume[$i][in_status]  = $rs[in_status];

   $i++;
}
$_SESSION[backurl] = '';

$_SESSION[backurl]  = $_SERVER[QUERY_STRING];
$_SESSION[backurl2] = "submitapplicant.php?".$_SESSION[backurl]; 
$smarty->assign("resumelist",$dataresume);
$smarty->assign("page",$page);
if($err!=''){
$smarty->assign("err",$err);
}
$smarty->assign("search",$reg_data);
$smarty->display('allleft.tpl');
$smarty->display('submitapplicant.tpl');
$smarty->display('footer.tpl');
?>